package com.yunche.yunchebusiness.ui.activity.finance

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.ViewGroup
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseItemDraggableAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.api.Api
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.base.BaseApp
import com.yunche.yunchebusiness.entity.Account
import com.yunche.yunchebusiness.entity.BankListEntity
import com.yunche.yunchebusiness.entity.TenantInfoEntity
import com.yunche.yunchebusiness.mvp.contract.AddTenantContract
import com.yunche.yunchebusiness.mvp.contract.TenantContract
import com.yunche.yunchebusiness.mvp.presenter.AddTenantPresenter
import com.yunche.yunchebusiness.mvp.presenter.TenantPresenter
import com.yunche.yunchebusiness.network.RequestJson
import com.yunche.yunchebusiness.ui.view.YuncheToolBar
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_my_card.*

class MyCardActivity : BaseActivity(), TenantContract.View, AddTenantContract.View {

    private var tenantEntity: TenantInfoEntity? = null
    private var mAdapter: CardAdapter? = null

    private val addBankPresenter by lazy {
        AddTenantPresenter()
    }

    private val mPresenter by lazy {
        TenantPresenter()
    }

    override fun getTenantSuccess(tenantEntity: TenantInfoEntity) {
        this.tenantEntity = tenantEntity
        mAdapter = CardAdapter(R.layout.item_bank_list, tenantEntity.accountList!!)
        rv_my_card.adapter = mAdapter
    }

    override fun showLoading() {
        srflay_card.isRefreshing = true
    }

    override fun hideLoading() {
        srflay_card.isRefreshing = false
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun getBankListSuccess(bankListEntity: List<BankListEntity>) {
    }

    override fun submitSuccess() {
        initData()
    }

    override fun setLayout(): Int = R.layout.activity_my_card

    override fun initView() {
        mPresenter.attachView(this)
        addBankPresenter.attachView(this)
        rv_my_card.layoutManager = LinearLayoutManager(mContext!!, LinearLayoutManager.VERTICAL, false)

        var swipeMenuCreator = SwipeMenuCreator { leftMenu, rightMenu, position ->
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val width = resources.getDimensionPixelSize(R.dimen.dp_72)
            var deleteItem = SwipeMenuItem(BaseApp.mContext)
            deleteItem.text = "删除"
            deleteItem.textSize = 14
            deleteItem.setTextColor(Color.WHITE)
            deleteItem.width = width
            deleteItem.height = height
            deleteItem.setBackgroundColor(Color.parseColor("#ef5350"))
            rightMenu.addMenuItem(deleteItem)
        }

        var mMenuItemClickListener = SwipeMenuItemClickListener { menuBridge, position ->
            menuBridge.closeMenu()
            submit(position)
        }
        rv_my_card.setSwipeMenuCreator(swipeMenuCreator)
        rv_my_card.setSwipeMenuItemClickListener(mMenuItemClickListener)
    }


    private fun submit(position: Int) {
        var accountList = JSONArray()

        tenantEntity?.accountList?.forEachIndexed { i, account ->
            if (i != position) {
                var accountJson = JSONObject()
                accountJson["account"] = account.account
                accountJson["accountName"] = account.accountName
                accountJson["bank"] = account.bank
                accountJson["accoutCardId"] = account.accoutCardId
                accountList.add(accountJson)
            }
        }
        var para = JSONObject()
        para["accountList"] = accountList
        addBankPresenter.submit(RequestJson.getRequestBody(para.toJSONString()), Api.getTenantId())
    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    override fun initData() {
        mPresenter.getTenantInfo()
    }

    override fun initEvent() {

        srflay_card.setOnRefreshListener {
            initData()
        }


        toolbar.yuncheToolbarCallBack = object : YuncheToolBar.YuncheToolbarCallBack {
            override fun onBackClick() {
                finish()
            }

            override fun onRightClick() {
                var bundle = Bundle()
                bundle.putSerializable("tenant", tenantEntity)
                openActivityWithBundle(AddBankActivity::class.java, bundle)
            }

        }
    }

    inner class CardAdapter constructor(id: Int, data: List<Account>) :
        BaseItemDraggableAdapter<Account, BaseViewHolder>(id, data) {
        override fun convert(helper: BaseViewHolder?, item: Account?) {

            var account = item?.account!!

            var bankNum = ""
            if (account.length > 8) {
                bankNum = account.substring(0, 4) + "  ****  " + "  ****  " +
                        account.substring(account.length - 4, account.length)
            }


            var idcard = item.accoutCardId!!
            var idcardStar = ""
            if (idcard.length > 8) {
                for (j in 0..idcard.length - 8) {
                    idcardStar += "*"
                }
            }

            helper?.setText(
                R.id.tv_bank_card_num, bankNum
            )
            helper?.setText(R.id.tv_bank_name, item?.bankName)
            helper?.setText(R.id.tv_account_name, item?.accountName)
            helper?.setText(
                R.id.tv_account_idcard,
                idcard.substring(0, 4) + "   " + idcardStar + "   " + idcard.substring(idcard.length - 4, idcard.length)
            )


            Glide.with(mContext).load(item?.bankIcon).into(helper!!.getView(R.id.iv_bank_icon))
        }
    }
}
